package br.com.agiledev.webscrum.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import br.com.agiledev.webscrum.entity.ItemRelease;

@Name("itemReleaseConverter")
@BypassInterceptors
@org.jboss.seam.annotations.faces.Converter(id = "itemReleaseConverter")
public class ItemReleaseConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
	return component.getAttributes().get(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
	
	if(value == null) {
	    return "";
	}
	
	ItemRelease ip = (ItemRelease) value;
	component.getAttributes().put(getId(ip), ip);
	
	return getId(ip);
    }
    
    private String getId(ItemRelease ir) {
	return ir.getItem().getOid() + ":" + ir.getRelease().getOid();
    }

}
